﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace SmartSpace.Web
{
    public class SsSite
    {
        private static SsSite _instance;

        public static string HostUrl { get; private set; }
        public static Site Current { get; private set; }
        public static SsSite Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SsSite();
                }
                return _instance;
            }
        }

        public Core.Nav CurrentNav
        {
            get
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                Core.Nav nav = service.GetNav(HttpContext.Current.Request.Url.PathAndQuery).Data;
                return nav;
            }
        }

        public Core.Page CurrentPage
        {
            get
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                Core.Page page = service.GetPage(Current.Id, HttpContext.Current.Request.Url.PathAndQuery);
                return page;
            }
        }

        static SsSite()
        {
            HostUrl = HttpContext.Current.Request.Url.OriginalString.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/");

            ICoreService service = ServiceLocator.Get<ICoreService>();
            Current = service.GetSite(HostUrl);
        }

        public static void ReLoad()
        {
            HostUrl = HttpContext.Current.Request.Url.OriginalString.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/");

            ICoreService service = ServiceLocator.Get<ICoreService>();
            Current = service.GetSite(HostUrl);
        }
    }
}
